/*
 * 方案管理Controller
 * @Author: Luo jiaxing 
 * @Date: 2017-05-09 10:44:27 
 * @Last Modified by: cgd
 * @Last Modified time: 2017-11-01 15:16:57
 */
Ext.define('Admin.view.solutionManage.SolutionManageController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.solutionManageController',
    views: ['Admin.view.solutionManage.manages.Year', 
    'Admin.view.solutionManage.manages.Month',
    'Admin.view.solutionManage.manages.Xun'],
    filterData: function () {
        this.loadTableData();
    },
    loadTableData: function (loading) {
        var store = this.lookupReference('solutionGrid').store;
        var startDate = this.lookupReference('start_date').getValue();
        var endDate = this.lookupReference('end_date').getValue();
        var user = this.lookupReference('user').getValue();
        var params = store.getProxy().getExtraParams();
        params.dateBegin = this.getDateStr(startDate);
        params.dateEnd = this.getDateStr(endDate);
        params.user = user;
        store.load();
    },
    getDateStr: function (date) {
        if (date) {
            var year = date.getFullYear();
            var month = date.getMonth() + 1;
            var month2 = month >= 10 ? month : '0' + month;
            var day = date.getDate();
            var day2 = day >= 10 ? day : '0' + day;
            return year + '-' + month2 + '-' +day2;
        } else {
            return '';
        }
    },
    solutionDelete: function (grid, rowIndex, colIndex) {
        var me = this;
        Ext.Msg.confirm("提示", "确定要删除吗？", function (button) {
            if (button === 'yes') {
                // grid.store.removeAt(rowIndex);
                $.ajax({
                    url: '/jsite-szy-dispatch/a/szy/dispatch/ddsRdP/remove',
                    method: 'POST',
                    contentType: 'application/json',
                    data: JSON.stringify({
                        proCd: grid.store.getAt(rowIndex).get('proCd')
                    }),
                    success: function (response) {
                        var proCd = response.id;
                        if (response.code === 1) {
                            me.loadTableData();
                        } else {
                            Ext.Msg.alert("提示信息", "删除方案失败，" + response.msg);
                        }
                    }
                });
            }
        })
    },
    toNewYearSolution: function () {
        var me = this;
        $.dispatch.setYearEdit(false);
        me.redirectTo('newYearSolution');
        var ysTab = Ext.getCmp('yearSolutionTab');
        if (ysTab) {
            ysTab.removeAll();
            ysTab.add({
                xtype: 'baseYearSolutionInfo'
            });
        }
    },
    toNewMonthSolution: function () {
        var me = this;
        $.dispatch.setMonthEdit(false);
        me.redirectTo('newMonthSolution');
        var msTab = Ext.getCmp('monthSolutionTab');
        if (msTab) {
            msTab.removeAll();
            msTab.add({
                xtype: 'baseMonthSolutionInfo'
            });
        }
    },
    toNewXunSolution: function () {
        var me = this;
        $.dispatch.setXunEdit(false);
        me.redirectTo('newXunSolution');
        var xsTab = Ext.getCmp('xunSolutionTab');
        if (xsTab) {
            xsTab.removeAll();
            xsTab.add({
                xtype: 'baseXunSolutionInfo'
            });
        }
    },
    yearSolutionEdit: function (grid, rowIndex, colIndex) {
        var me = this;
        var proCd = grid.store.getAt(rowIndex).get('proCd');
        $.dispatch.setYearProCd(proCd);
        $.dispatch.setYearEdit(true);
        $.dispatch.setYearView(false);
        me.redirectTo('newYearSolution');
        setTimeout(function () {
            var ysTab = Ext.getCmp('yearSolutionTab');
            if (ysTab) {
                ysTab.removeAll();
                ysTab.add({
                    xtype: 'yearDispatch'
                });
            }
        }, 0);
        
    },
    monthSolutionEdit: function (grid, rowIndex, colIndex) {
        var me = this;
        var proCd = grid.store.getAt(rowIndex).get('proCd');
        $.dispatch.setMonthProCd(proCd);
        $.dispatch.setMonthEdit(true);
        $.dispatch.setMonthView(false);
        me.redirectTo('newMonthSolution');
        setTimeout(function () {
            var msTab = Ext.getCmp('monthSolutionTab');
            if (msTab) {
                msTab.removeAll();
                msTab.add({
                    xtype: 'monthDispatch'
                });
            }
        }, 0);
    },
    xunSolutionEdit: function (grid, rowIndex, colIndex) {
        var me = this;
        var proCd = grid.store.getAt(rowIndex).get('proCd');
        $.dispatch.setXunProCd(proCd);
        $.dispatch.setXunEdit(true);
        $.dispatch.setXunView(false);
        me.redirectTo('newXunSolution');
        setTimeout(function () {
            var xsTab = Ext.getCmp('xunSolutionTab');
            if (xsTab) {
                xsTab.removeAll();
                xsTab.add({
                    xtype: 'xunDispatch'
                });
            }
        }, 0);
    },
    yearSolutionView: function (grid, rowIndex, colIndex) {
        var me = this;
        var proCd = grid.store.getAt(rowIndex).get('proCd');
        $.dispatch.setYearProCd(proCd);
        $.dispatch.setYearEdit(true);
        $.dispatch.setYearView(true);
        me.redirectTo('newYearSolution');
        setTimeout(function () {
            var ysTab = Ext.getCmp('yearSolutionTab');
            if (ysTab) {
                ysTab.removeAll();
                ysTab.add({
                    xtype: 'yearDispatch'
                });
            }
        }, 0);
        
    },
    monthSolutionView: function (grid, rowIndex, colIndex) {
        var me = this;
        var proCd = grid.store.getAt(rowIndex).get('proCd');
        $.dispatch.setMonthProCd(proCd);
        $.dispatch.setMonthEdit(true);
        $.dispatch.setMonthView(true);
        me.redirectTo('newMonthSolution');
        setTimeout(function () {
            var msTab = Ext.getCmp('monthSolutionTab');
            if (msTab) {
                msTab.removeAll();
                msTab.add({
                    xtype: 'monthDispatch'
                });
            }
        }, 0);
    },
    xunSolutionView: function (grid, rowIndex, colIndex) {
        var me = this;
        var proCd = grid.store.getAt(rowIndex).get('proCd');
        $.dispatch.setXunProCd(proCd);
        $.dispatch.setXunEdit(true);
        $.dispatch.setXunView(true);
        me.redirectTo('newXunSolution');
        setTimeout(function () {
            var xsTab = Ext.getCmp('xunSolutionTab');
            if (xsTab) {
                xsTab.removeAll();
                xsTab.add({
                    xtype: 'xunDispatch'
                });
            }
        }, 0);
    },
    formatSta0: function (val) {
        const sta = 0;
        return this.formatSta(val, sta);
    },
    formatSta1: function (val) {
        const sta = 1;
        return this.formatSta(val, sta);
    },
    formatSta2: function (val) {
        const sta = 2;
        return this.formatSta(val, sta);
    },
    formatSta3: function (val) {
        const sta = 3;
        return this.formatSta(val, sta);
    },
    formatSta4: function (val) {
        const sta = 4;
        return this.formatSta(val, sta);
    },
    formatSta: function (val, val2) {
        const completeText = '已完成';
        const inCompleteText = '未完成';
        const completeColor = '#00CC00';
        const inCompleteColor = '#FF0000';
        var statusTxet = val >= val2 ? completeText : inCompleteText;
        var textColor = '#333333';
        if (statusTxet === completeText) {
            textColor = completeColor;
        } else if (statusTxet === inCompleteText) {
            textColor = inCompleteColor;
        }
        return '<span style="color: ' + textColor + ';">' + statusTxet + '</span>';
    },
    viewSolution: function () {
        Ext.create('Ext.window.Window', {
            title: '方案详情',
            width: 900,
            height: 500,
//          modal: true,
            layout: 'fit',
            items: [{
                xtype: 'tabpanel',
                cls: 'mytab2-panel',
                tabBar: {
                    cls: 'mytab2',
                    style: 'border-bottom:1px solid #2A95C5 !important'
                },
                items: [{
                    title: '径流预报'
                }, {
                    title: '供需水计算'
                }, {
                    title: '用水分配'
                }, {
                    title: '水库调节'
                }, {
                    title: '结果分析'
                }]
            }]
        }).show();
    }
})